package com.kili.file.config.elect;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * List of parameters to be checked
 *
 * @author fenny
 * @since 2019/07/25
 */
@Getter
@Setter
@Slf4j
@Configuration
@PropertySource("classpath:valid.properties")
@ConfigurationProperties(prefix = "valid")
@ConditionalOnProperty(value = "custom.valid.enable", havingValue = "true")
public class ValidConfiguration {

    @Resource
    private Environment environment;

    private List<String> keys;


    /**
     * Check whether the specified key exists and there is no system termination
     */
    @PostConstruct
    public void valid() {
        Optional.ofNullable(keys).
                orElseThrow(() ->
                        new NullPointerException(
                                "System config loaded for error Not exists key  ,please check to your properties"
                        )).

                stream().anyMatch((var1) ->{
             if (environment.getProperty(var1) == null ||
                    this.internalElementValid(var1)) {
                log.error("Service value corresponding to value is not valid");System.exit(-1);
                return false ;
             }
             return true;
            //The  other valid
        });
//        map(var -> {
//            String var1 = environment.getProperty(var);
//            if (environment.getProperty(var1) == null ||
//                    this.internalElementValid(var1)) {
//                log.error("Service value corresponding to value is not valid");System.exit(-1);
//            }//The  other valid
//            return var;
//        });
    }


    public boolean internalElementValid(String ln) {
        boolean flag = false;
        int ci = ln.indexOf('#');
        if (ci >= 0) {
            ln = ln.substring(0, ci);
        }
        ln = ln.trim();
        int n = ln.length();
        if (n != 0) {
            if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) {
                log.error("Illegal configuration-file syntax");
                flag = true;
            }
            int cp = ln.codePointAt(0);
            if (!Character.isJavaIdentifierStart(cp)) {
                log.error("Illegal provider-service path: " + ln);
                flag = true;
            }
            for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
                cp = ln.codePointAt(i);
                if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) {
                    log.error("Illegal provider-service path: " + ln);
                    flag = true;
                }
            }

            return flag;
        }
    }
}
